package clock;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Clock extends JFrame{

	private static final long serialVersionUID = 1L;

	private final ClockCanvas canvas;
	private final Tick tick;
	private final Timer timer;
	
	public static int WIDTH = 400;
	public static int HEIGHT = 300;
	
	public Clock() {
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				Clock.this.timer.stop();
				System.exit(0);
			}
		});
		
		Insets insets = this.getInsets();
		this.canvas = new ClockCanvas(Clock.WIDTH, Clock.HEIGHT, (WIDTH-insets.left)/2, (HEIGHT-insets.top)/2);
		this.tick = new Tick(canvas);
		this.timer = new Timer(100, tick);
		
		this.add(canvas, "Center");
		this.setTitle("TestClock");
		
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		this.setBounds((int) ((dimension.getWidth()-Clock.WIDTH) / 2), (int) ((dimension.getHeight()-Clock.HEIGHT)/2), Clock.WIDTH, Clock.HEIGHT);
		
		this.timer.start();
	}
	public static void main(String[] args) {
		new Clock();
	}
}
